#ifndef GAME_ANIMATION_MOON_SHADER_H
#define GAME_ANIMATION_MOON_SHADER_H
//
// Game Animation.
// Copyright (C) 2007 Department of Computer Science, University of Copenhagen
//
#include <animation/configuration.h>

#include <OpenTissue/gpu/cg/cg_program.h>
#include <OpenTissue/gpu/image/image.h>
#include <OpenTissue/gpu/image/io/image_read.h>
#include <OpenTissue/gpu/image/io/image_write.h>
#include <OpenTissue/gpu/texture/texture_texture2D.h>
#include <OpenTissue/gpu/image/image_alpha_modulation.h>

namespace animation
{

  /**
  * The Moon Shader.
  * Sets up rendering before drawing terrain geometry.
  */
  class MoonShader
  {

  protected:

    OpenTissue::cg::Program       m_vertex_program;
    OpenTissue::cg::Program       m_fragment_program;

    boost::shared_ptr<OpenTissue::image::Image<unsigned char> >  m_moon_image;
    OpenTissue::texture::texture2D_pointer                       m_moon_textures;
  public:

	  void pre_render(
	  bool reload
	  ,float time
	  ,float day_length
	  , float rightx, float righty , float rightz
      , float upx, float upy, float upz
	  , float fog_density
	  , float fog_color_r, float fog_color_g, float fog_color_b
	  ){

		  if(!m_moon_image){
			  m_moon_image.reset( new OpenTissue::image::Image<unsigned char>() );
			  OpenTissue::image::read(demo_path + "TheSwampThing2008/data/moon_phase.png",*m_moon_image);
			  m_moon_textures = m_moon_image->create_texture(GL_RGBA);
			  m_moon_textures->set_repeating(true);
			  m_moon_textures->set_mipmapping(true);
		  }	
		  if(!m_vertex_program.is_program_loaded()||reload)
			  m_vertex_program.load_from_file(OpenTissue::cg::Program::vertex_program,demo_path + "../animation/shader/moon/moon_vp.cg");

		  float days25=day_length*25.0;
		  float day=floor((time-floor(time/days25)*days25)/day_length);
		  float row=floor(day/5.0);//row is 0-4;
		  float v=1.0-(row+1.0)*0.20;//v row is 0.80 to 0 
		  float u=floor(day-row*5.0)/5.0;//col is 0 to 0.80
      m_vertex_program.set_modelview_projection_matrix();
      m_vertex_program.set_float_param("right",               rightx, righty, rightz);
	  m_vertex_program.set_float_param("up",                  upx,    upy,    upz);
	  m_vertex_program.set_float_param("uv_offset",                  u,    v);
      //--- load fragment program
      if(!m_fragment_program.is_program_loaded()||reload)
        m_fragment_program.load_from_file(OpenTissue::cg::Program::fragment_program,demo_path + "../animation/shader/moon/moon_fp.cg");

	  m_fragment_program.set_input_texture("moon_texture",m_moon_textures);
	  m_fragment_program.set_float_param( "fog_density",  fog_density  );
	  m_fragment_program.set_float_param( "fog_color",  fog_color_r,fog_color_g,fog_color_b);
      m_vertex_program.enable();
      m_fragment_program.enable();
    }

    void enable()
    {
      m_vertex_program.enable();
      m_fragment_program.enable();
    }
    void disable()
    {
      m_vertex_program.disable();
      m_fragment_program.disable();
    }
    void post_render()
    {
      m_fragment_program.disable();
      m_vertex_program.disable();
    }

  };

} // namespace animation

// GAME_ANIMATION_TERRAIN_SHADER_H
#endif
